<?php
/**
 * common functions
 *
 * @copyright		The XOOPS project http://www.xoops.org/
 * @license			http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
 * @package			installer
 * @since			2.0.18
 * @version			$Id: functions.php 604 2006-07-01 22:28:37Z skalpa $
 *
 * @author			Haruki Setoyama  <haruki@planewave.org>
 * @author			Kazumi Ono <webmaster@myweb.ne.jp>
 * @author			Skalpa Keo <skalpa@xoops.org>
 * @author			dugris <dugris@frxoops.org>
**/

function xoDiag( $status = -1, $str = '' ) {
	if ( $status == -1 ) {
		$GLOBALS['error'] = true;
	}
	$classes = array( -1 => 'error', 0 => 'warning', 1 => 'success' );
	$strings = array( -1 => FAILED, 0 => WARNING, 1 => SUCCESS );
	if ( empty($str) ) {
		$str = $strings[$status];
	}
	return '<span class="' . $classes[$status] . '">' . $str . '</span>';
}

function xoDiagBoolSetting( $name, $wanted = false, $severe = false ) {
	$setting = strtolower( ini_get( $name ) );
	$setting = ( empty( $setting ) || $setting == 'off' || $setting == 'false' ) ? false : true;
	if ( $setting == $wanted ) {
		return xoDiag( 1, $setting ? 'ON' : 'OFF' );
	} else {
		return xoDiag( $severe ? -1 : 0, $setting ? 'ON' : 'OFF' );
	}
}

function xoDiagIfWritable( $path ) {
	$path = "../" . $path;
	$error = true;
	if ( !is_dir( $path ) ) {
		if ( file_exists( $path ) ) {
			@chmod( $path, 0666 );
			$error = !is_writeable( $path );
		}
	} else {
		@chmod( $path, 0777 );
		$error = !is_writeable( $path );
	}
	return xoDiag( $error ? -1 : 1, $error ? 'Not writable' : 'Writable' );
}

function xoPhpVersion() {
	if ( version_compare( phpversion(), '4.3.0', '>=') ) {
		return xoDiag( 1, phpversion() );
	} elseif ( version_compare( phpversion(), '4.2.1', '>=') ) {
		return xoDiag( 0, phpversion() );
	} else {
		return xoDiag( -1, phpversion() );
	}
}

function xoFormField( $name, $value, $label, $help = '' ) {
	$label = htmlspecialchars( $label, ENT_QUOTES );
	$name = htmlspecialchars( $name, ENT_QUOTES );
	$value = htmlspecialchars( $value, ENT_QUOTES );
	$help = htmlspecialchars( $help, ENT_QUOTES );
	echo "<label for='$name'>$label";
	if ( $help ) {
		echo "<img class='Tips_$name' id='help_button' src='img/contexthelp.png' title='$label :: $help' class='off' />";
		$GLOBALS['wizard']->helper[] = "Tips_$name";
	}
	echo "</label>\n";
	echo "<input type='text' name='$name' id='$name' value='$value' />";
}

function xoPassField( $name, $value, $label, $help = '' ) {
	$label = htmlspecialchars( $label, ENT_QUOTES );
	$name = htmlspecialchars( $name, ENT_QUOTES );
	$value = htmlspecialchars( $value, ENT_QUOTES );
	$help = htmlspecialchars( $help, ENT_QUOTES );
	echo "<label for='$name'>$label";
	if ( $help ) {
		echo "<img class='Tips_$name' id='help_button' src='img/contexthelp.png' title='$label :: $help' />";		$GLOBALS['wizard']->helper[] = "Tips_$name";
	}
	echo "</label>\n";
	if ( $name == "adminpass" ) {
		echo "<input type='password' name='$name' id='$name' value='$value' onkeyup='passwordStrength(this.value)' />";
	} else {
		echo "<input type='password' name='$name' id='$name' value='$value' />";
	}

}

function getDirList($dirname) {
	$dirlist = array();
	if ( $handle = opendir($dirname) ) {
		while ( $file = readdir($handle) ) {
			if ( $file{0} != '.' && is_dir( $dirname . $file ) ) {
				$dirlist[] = $file;
			}
		}
		closedir($handle);
		asort($dirlist);
		reset($dirlist);
	}
	return $dirlist;
}
?>